package com.yonyougov.devops.cruxcisupport.conver.commitcontext;

import com.yonyougov.devops.cruxcisupport.app.AppDao;
import com.yonyougov.devops.cruxcisupport.app.AppEntity;
import com.yonyougov.devops.cruxcisupport.commitcontext.CommitContextDao;
import com.yonyougov.devops.cruxcisupport.commitcontext.CommitContextEntity;
import com.yonyougov.devops.cruxcisupport.commitdevelopuser.CommitDevelopUserDao;
import com.yonyougov.devops.cruxcisupport.commitdevelopuser.CommitDevelopUserEntity;
import com.yonyougov.devops.cruxcisupport.user.UserDao;
import com.yonyougov.devops.cruxcisupport.util.excell.CruxCommitTestContentEntity;
import com.yonyougov.devops.cruxcisupport.version.VersionDao;
import com.yonyougov.devops.cruxcisupport.version.VersionEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

@Component
public class CommitContextEntity2CruxCommitTestContentEntity {

    private final CommitContextDao commitContextDao;
    private final AppDao appDao;
    private final VersionDao versionDao;
    private final UserDao userDao;
    private final CommitDevelopUserDao commitDevelopUserDao;

    public CommitContextEntity2CruxCommitTestContentEntity(CommitContextDao commitContextDao, AppDao appDao, VersionDao versionDao, UserDao userDao, CommitDevelopUserDao commitDevelopUserDao) {
        this.commitContextDao = commitContextDao;
        this.appDao = appDao;
        this.versionDao = versionDao;
        this.userDao = userDao;
        this.commitDevelopUserDao = commitDevelopUserDao;
    }


    public List<CruxCommitTestContentEntity> batchCov(List<CommitContextEntity> list){
        List<CruxCommitTestContentEntity> resultList = new ArrayList<>();
        for(CommitContextEntity commitContextEntity : list){
            // 应用
            AppEntity appEntity = appDao.findByPrimaryKey(commitContextEntity.getAppId());
            // 版本
            VersionEntity versionEntity = versionDao.findBy(VersionEntity.builder().id(commitContextEntity.getVersionId()).build());
            // 开发者
            CommitDevelopUserEntity commitDevelopUserEntity = new CommitDevelopUserEntity();
            commitDevelopUserEntity.setCommitContextId(commitContextEntity.getId());
            List<CommitDevelopUserEntity> commitDevelopUserEntities = commitDevelopUserDao.findAllBy(commitDevelopUserEntity);
            Set<BigInteger> userIds = commitDevelopUserEntities.stream().map(CommitDevelopUserEntity::getDevelopUserId).collect(Collectors.toSet());
            Optional<String> userNameStrings = userDao.findByIds(userIds).stream().map(e -> {
                if (StringUtils.isNotBlank(e.getNickName())) {
                    return e.getNickName();
                } else {
                    return e.getUserName();
                }
            }).reduce(new BinaryOperator<String>() {

                @Override
                public String apply(String s, String s2) {
                    return s + "; " + s2;
                }
            });
            // 分支
            String testBranch = commitContextEntity.getTestBranch();
            // 备注
            String remark = commitContextEntity.getRemark();
            resultList.add(
                CruxCommitTestContentEntity.builder()
                        .appName(appEntity.getApp())
                        .version(versionEntity.getVersionName())
                        .branchName(testBranch)
                        .developer(userNameStrings.orElse("未填写"))
                        .remark(remark).build()
            );
        }
        return resultList;
    }

}
